package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/merchant/user")
public class SysMerchantUserController extends BaseController {

    @Autowired
    private ISysRoleService roleService;
    @Autowired
    private ISysUserService userService;

    //TODO （待优化）可以将循环修改优化在sql拼接上，只进行一次修改sql语句的操作调用
    @PreAuthorize("@ss.hasPermi('system:user:list')")
    @PostMapping("/dismissal/{userIds}")
    public AjaxResult dismissal(@PathVariable Long[] userIds) {
        List<SysUser> userList=userService.selectUserByIds(userIds);
        SysRole sysRole=new SysRole();
        sysRole.setRoleKey("common");
        int count=0;
        List<SysRole> roleList= roleService.selectAllRoleList(sysRole);
        for (SysUser user:userList) {
            user.setRoleIds(new Long[]{roleList.get(0).getRoleId()});
            user.setDeptId(100L);
            count+=userService.updateUser(user);
        }
        return success(count);
    }
}
